package GoldenEagle;
/**
 * file IO class for reading txt file
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

public class DataProvider {
	private static ArrayList<String> strStore;


public static ArrayList<String> getDataStore(){return strStore;}
//when fulfill this condition ,the group of data read end
public static boolean dataEndCondition(int index)
{
	try{
		
		if(index >= strStore.size())	return true;	
		String rawStringData = strStore.get(index);
		return 	(rawStringData.startsWith("[") || rawStringData.isEmpty());
	}catch(NullPointerException e)
	{
		System.out.println("file not prased.");
	}
	return false;
}
/**
 * load data from file to the DataProvider
 * @param txtFile
 */
public static void praseFromFile(File txtFile)
{
	BufferedReader reader;
	String tempString;
	ArrayList<String> store = new ArrayList<String>();
	try {
		reader = new BufferedReader(new FileReader(txtFile));
		while((tempString = reader.readLine()) != null){
			store.add(tempString);
		}
		
	} catch (Exception e1) {
		e1.printStackTrace();
	}
	strStore = store;
	
}
/**
 * search tag from txt file and get the data
 * @param tag
 * @return
 */
public static String getData(String tag){
	try
	{
		String output = "";
		for(int i=0;i<strStore.size();i++)
		{
			String temp = strStore.get(i);
		
			if(temp.equals("["+tag+"]")){
				//index of start reading data
			int idx;
			
			for(idx = i+1;!dataEndCondition(idx);idx++){
				temp = strStore.get(idx);
				output += temp;
			}
			i = idx;
		}
			
		}
		return output;
	}catch(NullPointerException e)
	{
		System.out.println("file not prased.");
	}
	return "Error";

}
}